<template>
    <div class="page">
        <div class="header">
            <div class="header-wrapper">
                <div v-for="(data, index) in titleList" 
                :key="index" @click="handTitle(index)" :class="handIndex === index ? 'active' : ''" class="header-wrap">{{ data }}</div>
            </div>
        </div>
        <div class="con">
            <div class="con-wrap" v-for="(data, index) in titleList" 
                :key="index" :style="{ left: index * 100 + '%' }">
                <div class="con-wrapper mt-xs" :style="{ left: '-' + (handIndex * 100) + '%' }">
                    <template v-if="index === 0">
                        <!-- 立减金 tab第一 -->
                        <div class="content" v-if="id === '1'">
                            <div class="price-img" v-for="(data, index) in knockList" :key="index">
                                <div class="price-left">
                                    <span class="price-wrap">￥<span>{{ data.coupon_price }}</span></span>
                                </div>
                                <div class="price-right">
                                    <div class="price-top">立减金</div>
                                    <div class="price-bot">无门槛立减</div>
                                </div>
                            </div>
                        </div>
                        <!-- 特权金 -->
                        <div class="content" v-if="id === '2'">
                            <router-link v-for="(data,index) in privilegeList" :key="index" @click="handPrivilegeInfo" style="position: relative; margin-bottom: .2rem;" :to="`/activity/list?type=privilege&id=${data.cid}`">
                                <div class="content-wrapper" v-if="!privilegeList.length">
                                    <p>还差99个金币即可兑换</p>
                                    <div class="content-con">马上赚金币</div>
                                </div>
                                <div style="position: relative;" class="w-full">
                                    <img src="@/assets/images/1.1.png" alt="coupon-background">
                                    <div style="position: absolute; top: 0; bottom: 0; margin: auto" class="w-full type-flex justify-between font-color-white">
                                        <div class="coupon-money"><span>￥</span><span style="font-size: .66rem;">{{ parseInt(data.coupon_price) }}</span></div>
                                        <div class="type-flex justify-between">
                                            <div>
                                                <div><span v-if="data.id !== 3" class="font-bold">{{ 100 }}</span> {{ data.id === 3 ? `限单首用，除拼团` : `金币兑换` }}</div>
                                                <div class="font-xs" v-if="data.countdown">{{ `${data.countdown.day > 0 ? data.countdown.day+':' : '' }${data.countdown.hour}:${data.countdown.min}:${data.countdown.sec}.${data.countdown.ms}` }}后作废</div>
                                                <div class="font-xxs">多款特权金标志商品{{ data.id === 3 ? `首单` : `支持` }}使用</div>
                                            </div>
                                            <div><span class="btn-sm">立即使用</span></div>
                                        </div>
                                    </div>
                                </div>
                            </router-link>
                        </div>
                        <!-- 特权卡 -->
                        <div class="content" v-if="id === '3'">
                            <template v-if="privilegeList.cantuan && privilegeList.cantuan.length > 0">
                                <div  class="content-img-wrap" v-for="(data, index) in privilegeList.cantuan" :key="index">
                                    <div class="content-img-flex">
                                        <div class="content-img-font">{{ data.name }}</div>
                                    </div>
                                </div>
                            </template>
                            <div v-else class="content">抱歉，暂无数据</div>
                        </div>
                    </template>
                    <!-- tab第二 -->
                    <template v-if="index === 1">
                        <div class="content">
                            <template v-if="privilegeList.kanjia && privilegeList.kanjia.length > 0">
                                <div class="content-img-wrap" v-for="(data, index) in privilegeList.kanjia" :key="index">
                                    <div class="content-img-flex">
                                        <div class="content-img-font">{{ data.name }}</div>
                                    </div>
                                </div>
                            </template>
                            <div v-else class="content">抱歉，暂无数据</div>
                        </div>
                    </template>
                    <!-- tab第三 -->
                    <template v-if="index === 2">
                        <div class="content">
                            <template v-if="privilegeList.pintuan && privilegeList.pintuan.length > 0">
                                <div class="content-img-wrap" v-for="(data, index) in privilegeList.pintuan" :key="index">
                                    <div class="content-img-flex">
                                        <div class="content-img-font">{{ data.name }}</div>
                                    </div>
                                </div>
                            </template>
                            <div v-else class="content">抱歉，暂无数据</div>
                        </div>
                    </template>
                </div>    
            </div>
        </div>
    </div>
</template>

<script>
    import { myChangeList, getReduction, getCouponsUser } from "@/api/user"
    import { countdown } from "@utils/common.js";
    export default {
        data() {
            return {
                handIndex: 0,
                titleList: ['未使用'],
                id: 0,
                privilegeList: [],
                knockList: [],
                timer: null,
            }
        },
        created() {
            this.init();
        },
        mounted() {
            // console.log(this.$route.params.id)
        },
        destroyed() {
            if (this.privilegeList.length) {
                clearInterval(this.timer);
            }
        },
        methods: {
            init() {
                this.id = this.$route.params.id;
                // 立减金
                if (this.id === '1') {
                    this.getReductionPort();
                }
                // 特权金
                if (this.id === '2') {
                    this.getCouponsUserPort();
                }
                // 特权卡
                if (this.id === '3') {
                    this.myChangeListPort();
                    this.titleList = ['参团卡', '砍价卡', '特权卡'];
                }
            },
            /**
             * 特权金接口
             */
            async getCouponsUserPort() {
                const data = await getCouponsUser(1).catch(error => console.error(error));
                if (!data.data.length) return;
                for(let key in data.data) {
                    data.data[key].countdown = {};
                }
                this.privilegeList = data.data;
                
                let that = this, ms = 10;
                this.timer = setInterval(() => {
                    ms--;    
                    if (ms === -1) {
                        ms = 9;
                    }
                    for(let key in that.privilegeList) {
                        let endTime = Number(that.privilegeList[key].end_time+'000');
                        that.privilegeList[key].countdown = countdown(endTime, ms);
                    }
                }, 100);
            },
            /**
             * 查看立减金
             */
            async getReductionPort() {
                const data = await getReduction().catch(error => {
                    this.knockList = [];
                });
                this.knockList = data.data;
            },
            /**
             * 特权卡接口
             */
            async myChangeListPort() {
               const data = await myChangeList().catch(error => console.error(error));
               this.privilegeList = data.data;
            },
            handTitle(index) {
                this.handIndex = index;
            }
        }
    }
</script>

<style scoped>
    .page {
        min-height: 100vh;
        background: rgb(245,245,245);
    }
    .header {
        width: 100%;
        background: #fff;
        height: 7vh;
    }

    .header-wrapper {
        display: flex;
        width: 80%;
        margin: 0 auto;
        justify-content: space-around;
        align-items: center;
        height: 100%;
        color: rgb(88,88,88);
    }

    .header-wrap {
        height: 100%;
        position: relative;
        display: flex;
        align-items: center;
    }

    .active::after {
        content: "";
        width: 50px;
        height: 2px;
        background: rgb(121,64,137);
        position: absolute;
        bottom: 0;
        left: -3px;
    }

    .con {
        padding: 20px;
        position: relative;
        overflow: hidden;
        overflow-y: auto;
        min-height: 93vh;
    }

    .con-wrap {
        position: absolute;
        top: 0;
        transition: all .5s;
        width: 100%;
    }

    .con-wrapper {
        position: relative;
        width: 100%;
        transition: all .3s;
    }

    .content {
        width: 95%;
        margin: 0 auto;
        height: auto;
        background: #fff;
        border-radius: 6px;
        /* margin-top: -50px; */
        padding: 10px;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: rgb(161,161,161);
        flex-direction: column;
    }

    .content-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;
    }

    .content-price {
        font-weight: 700;
    }

    .content-info {
        font-size: 12px;
        color: rgb(161,161,161);
    }

    .content-wrap {
        margin-top: 0;
    }

    .content-position {
        width: 100%;
        height: 80px;
        position: relative;;
    }

    .content img {
        width: 100%;
        height: 80px;
    }

    .content-wrapper {
        width: 100%;
        height: 80px;
        background: rgba(0,0,0,.6);
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        border-radius: .06rem;
        z-index: 3;
    }

    .content-wrapper p {
        color: #ffffff;
        font-size: 12px;
        font-weight: 700;
        margin-bottom: 5px;
    }

    .content-con {
        border: 1px solid #ffffff;
        color:#fff;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50px;
        padding: 2px 5px;
        font-size: 12px;
        font-weight: 700;
    }
    .content-img {
        width: 100%;
        height: 80px;
        background: url("../../assets/images/1.1.png") no-repeat;
        background-size: 100%;
        position: relative;
    }

    .img-font {
        position: absolute;
        color: rgb(255,234,242);
        left: 86px;
        top: 7px;
        font-size: 14px;
    }

    .img-text {
        width: 120px;
        top: 42px;
    }

    .content-img {
        width: 100%;
        height: 80px;
        background: url("../../assets/images/1.1.png") no-repeat;
        background-size: 100%;
        position: relative;
        margin-bottom: 10px;
    }
    .img-price {
        position: absolute;
        left: 29px;
        font-size: 30px;
        color: #fff;
        top: 20px;
    }

    .img-font {
        position: absolute;
        color: rgb(255,234,242);
        left: 86px;
        top: 7px;
        font-size: 14px;
    }

    .img-time {
        position: absolute;
        color: rgb(255,234,242);
        left: 86px;
        /* top: 25px; */
        font-size: 12px;
        bottom: 8px;
    }

    .img-text {
        width: 120px;
        top: 42px;
    }
    
    .content-img-flex {
        width: 100%;
        height: 80px;
        background: url("../../assets/images/2.1.png") no-repeat;
        background-size: 100% 100%;
        position: relative;
    }

    .content-img-font {
        font-size: 21px;
        color: #fff;
        position: absolute;
        top: 24px;
        left: 23px;
    }

    .content-img-wrap {
        width: 100%;
        margin-bottom: 10px;
    }

    .price-img {
        width: 100%;
        height: 55px;
        background: linear-gradient(to left, rgb(251,99,85), rgb(253,79,75));
        display: flex;
    }

    .price-left {
        width: 30%;
        display: flex;
        align-items: center;
        justify-content: center;
        border-right: 1px solid rgb(254,199,186);
    }

    .price-left span {
        color: #fff;
        font-size: 30px;
        font-weight: 700;
    }

    .price-left .price-wrap {
        font-size: 16px;
    }

    .price-right {
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding-left: 10px;
    }

    .price-top {
        color: #fff;
    }

    .price-bot {
        color: #fff;
    }



    .coupon-money {
        padding: 0 4%;
        margin-right: 3%;
        border-right: 1px dashed #ffe8d4;
    }

    .btn-sm {
        display: block;
        min-width: 1.6rem;
        padding: .14rem .26rem;
        margin-right: .3rem;
        text-align: center;
        font-size: .2rem;
        color: rgba(255,124,78,1);
        letter-spacing: .02rem;
        background-color: #fff;
        border-radius: 5rem;
        box-shadow: 2px 2px 10px rgb(253,79,75);
    }
</style>